﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DSheet.Core.Data
{
    public class DimData
    {
        public Guid Id { get; set; }
        public string Code { get; set; }
        public string Name { get; set; }

        public List<DimItem> Items { get; set; }

        public DimData()
        {
            Items = new List<DimItem>();
        }

        public List<DimRowItem> Complanate()
        {
            List<DimRowItem> rows = new List<DimRowItem>();

            Stack<DimItem> stack = new Stack<DimItem>();
            for (int index = Items.Count - 1; index >= 0; index--)
            {
                stack.Push(Items[index]);
            }

            while (stack.Count > 0)
            {
                DimItem node = stack.Pop();

                DimRowItem item = new DimRowItem();

                item.Code = node.Code;
                item.Name = node.Name;
                item.Level = node.Level;
                rows.Add(item);
                //row.Items[0].Formula = node.Formula;

                for (int index = node.Items.Count - 1; index >= 0; index--)
                {
                    stack.Push(node.Items[index]);
                }
            }

            return rows;
        }

        #region 将维度数据转换为维度树
        public DTree ConvertToTree()
        {
            DTree tree = new DTree();

            for (int index = 0; index < Items.Count; index++)
            {
                ConvertNode(tree, Items[index]);
            }
            return tree;
        }

        private void ConvertNode(DTree tree, DimItem dataNode)
        {
            DTreeNode treeNode = new DTreeNode();
            treeNode.Tree = tree;
            treeNode.Level = dataNode.Level;
            treeNode.Code = dataNode.Code;
            treeNode.Name = dataNode.Name;
            //treeNode.Formula = dataNode.Formula;

            for (int index = 0; index < dataNode.Items.Count; index++)
            {
                DimItem subDataNode = dataNode.Items[index];
                ConvertNode(treeNode, subDataNode);
            }
            tree.Nodes.Add(treeNode);
        }

        private void ConvertNode(DTreeNode node, DimItem dataNode)
        {
            DTreeNode treeNode = new DTreeNode();
            treeNode.Tree = node.Tree;
            treeNode.Level = dataNode.Level;
            treeNode.Code = dataNode.Code;
            treeNode.Name = dataNode.Name;
            //treeNode.Formula = dataNode.Formula;

            for (int index = 0; index < dataNode.Items.Count; index++)
            {
                DimItem subDataNode = dataNode.Items[index];
                ConvertNode(treeNode, subDataNode);
            }
            node.Nodes.Add(treeNode);
        }
        #endregion
    }

    public class DimItem
    {
        public Guid Id { get; set; }
        public string Code { get; set; }
        public string Name { get; set; }
        public int Level { get; set; }
        public Guid ParentId { get; set; }
        public Guid DataId { get; set; }

        public List<DimItem> Items { get; set; }
        
        public DimItem()
        {
            Items = new List<DimItem>();
        }

        public void AddItem(DimItem item)
        {
            item.Level = this.Level + 1;
            this.Items.Add(item);
        }
    }

    public class DimRow
    {
        public int RowIndex { get; set; }
        public DimRowItem[] Items { get; set; }
    }

    public class DimRowItem
    {
        public string Code { get; set; }
        public string Name { get; set; }
        public int Level { get; set; }
    }
}
